package com.atguigu.daijia.customer.service.impl;

import com.atguigu.daijia.common.constant.RedisConstant;
import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.common.util.AuthContextHolder;
import com.atguigu.daijia.customer.client.CustomerInfoFeignClient;
import com.atguigu.daijia.customer.service.CustomerService;
import com.atguigu.daijia.model.form.customer.UpdateWxPhoneForm;
import com.atguigu.daijia.model.vo.customer.CustomerLoginVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class CustomerServiceImpl implements CustomerService {

    //注入远程调用接口
    @Autowired
    private CustomerInfoFeignClient client;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 小程序登录接口具体实现部分
     * @param code
     * @return
     */
    @Override
    public String login(String code) {
        //1.拿着code进行远程调用，最终返回用户Id
        Result<Long> longResult = client.login(code);
        //2.判断如果失败了，返回错误提示
        Integer codeResult = longResult.getCode();
        if (codeResult != 200){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        //3.获取远程调用的用户Id
        Long customerId = longResult.getData();
        //4.判断返回的用户Id是否为空，如果为空，返回错误提示
        if (customerId == null) {
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        //5.生成token字符串
        String token = UUID.randomUUID().toString().replaceAll("-","");
        //6.把用户id放到Redis上，设置过期时间
        // key: token value:customerId
        //redisTemplate.opsForValue().set(token,customerId,30, TimeUnit.MILLISECONDS);
        redisTemplate.opsForValue().set(RedisConstant.USER_LOGIN_KEY_PREFIX+token,
                customerId.toString(),
                RedisConstant.USER_LOGIN_KEY_TIMEOUT,
                TimeUnit.SECONDS);
        //7.返回token
        return token;
    }

    /**
     * 小程序获取用户信息
     * @param token
     * @return
     */
    @Override
    public CustomerLoginVo getCustmoerLoginInfo(String token) {
        /**
         * 不使用ThreadLocal获取用户ID
         */
//        //2.根据token查询redis
//        String customerId = (String) redisTemplate.opsForValue()
//                .get(RedisConstant.USER_LOGIN_KEY_PREFIX + token);
//        //3.查询token在Redis里面对应的用户id
//        if (!StringUtils.hasText(customerId)) {
//            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
//        }
//        //4.根据用户id进行远程调用，得到用户信息
        Long customerId = AuthContextHolder.getUserId();
        //5.将用户信息返回
        Result<CustomerLoginVo> customerLoginVoResult = client.getCustomerLoginInfo(customerId);
        if(customerLoginVoResult.getCode() != 200) {
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        CustomerLoginVo customerLoginVo = customerLoginVoResult.getData();
        if(customerLoginVo == null){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        return customerLoginVo;
    }

    //更新用户微信手机号
    @Override
    public Boolean updateWxPhoneNumber(UpdateWxPhoneForm updateWxPhoneForm) {
        Result<Boolean> booleanResult = client.updateWxPhoneNumber(updateWxPhoneForm);
        return true;
    }
}
